package au.com.iglootech.TaxTimeToolTip.ui.company;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;

import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction = true)
@Component
@Scope("request")
public class ManageCompanyTab extends CustomComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9049253248140023639L;
	@Autowired
	private transient CompanyService companyService;

	private VerticalLayout mainLayout = new VerticalLayout();
	private BeanContainer<String, Company> beans;

	public ManageCompanyTab() {
		setCaption("Manage Company");
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		beans = new BeanContainer<String, Company>(
				Company.class);

		// Use the name property as the item ID of the bean
		List<Company> companies = companyService.findAllCompany();
		beans.setBeanIdProperty("key");
		for(Company company : companies)
		{
			beans.addBean(company);
		}

		Table companyTable = new Table("Company List", beans);
		companyTable.setVisibleColumns(new String[]{"companyName", "contactName" ,"contactNumber", "tfn", "email", "address1", "address2", "address3"});
		companyTable.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
		companyTable.setWidth("100%");
		companyTable.setHeight("500px");
		panel.addComponent(companyTable);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}

	public void refresh()
	{
		List<Company> companies = companyService.findAllCompany();
		beans.removeAllItems();
		for(Company company : companies)
		{
			beans.addBean(company);
		}
	}
}
